<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
			<uni-forms-item name="type" label="优惠类型">
				<uni-data-checkbox v-model="formData.type" :localdata="sex"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="name" label="优惠名称">
				<uni-easyinput placeholder="优惠卷名称" v-model="formData.name" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="price" label="金额|比例">
				<uni-easyinput placeholder="满减金额:100为1元 | 折扣比例:80为8折,74为7.4折" type="number"
					v-model="formData.price"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="user_type" label="指定范围">
				<uni-data-checkbox v-model="formData.user_type" :localdata="sexs"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item v-if="formData.user_type == '指定用户'" name="user_arr" label="选择用户">
				<uni-data-checkbox mode="tag" multiple v-model="formData.user_arr" collection="uni-id-users"
					field="_id as value, nickname as text" selectedTextColor="#fff !important" selected-color="#3a7fff"
					mptyText="暂无用户"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="order_amount" label="实付金额">
				<uni-easyinput placeholder="订单实付多少可用,100等于1元" type="number"
					v-model="formData.order_amount"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="number" label="发放数量">
				<uni-easyinput placeholder="用户开通会员后送多少张" type="number" v-model="formData.number"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item style="display: none;" name="received_number" label="">
				<uni-easyinput placeholder="已领取数量" type="number" v-model="formData.received_number"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="allow_type" label="使用范围">
				<uni-data-checkbox v-model="formData.allow_type" :localdata="sexss"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item v-if="formData.allow_type == '指定商品'" name="goods_arr" label="选择商品">
				<uni-data-checkbox mode="tag" multiple v-model="formData.goods_arr" collection="opendb-mall-goods"
					where="'is_on_sale' == true" field="_id as value, name as text" selectedTextColor="#fff !important"
					selected-color="#3a7fff" mptyText="暂无商品"></uni-data-checkbox>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/jlg-goods-couponrule.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'jlg-goods-couponrule';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"type": "",
				"name": "",
				"price": null,
				"user_type": "",
				"user_arr": [],
				"order_amount": null,
				"max_number": null,
				"number": null,
				"received_number": 0,
				"start_time": null,
				"end_time": null,
				"allow_type": "",
				"goods_arr": [],
				"is_share": false,
				"state": ""
			}
			return {
				formData,
				formOptions: {},
				sex: [{
						text: "满减",
						value: "满减"
					},
					{
						text: "折扣",
						value: "折扣"
					}
				],
				sexs: [{
						text: "vip",
						value: "vip"
					},
					{
						text: "svip",
						value: "svip"
					}
				],
				sexss: [{
						text: "所有商品",
						value: "所有商品"
					},
					{
						text: "指定商品",
						value: "指定商品"
					}
				],
				sexsss: [{
						text: "待发放",
						value: "待发放"
					},
					{
						text: "已发放",
						value: "已发放"
					},
					{
						text: "已过期",
						value: "已过期"
					}
				],
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			}
		}
	}
</script>